package com.yjf.controller;

import com.yjf.pojo.LagouToken;
import com.yjf.pojo.UserInfo;
import com.yjf.response.CommonRespBuilder;
import com.yjf.response.ICommonResponse;
import com.yjf.service.LagouTokenService;
import com.yjf.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.UUID;

/**
 * @author Administrator 岳剑飞
 * @date
 * @description:
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private LagouTokenService lagouTokenService;



    @Value("${server.port}")
    private Integer port;

    //注册账号   注册接⼝，true成功，false失败
    @PostMapping("/register/{email}/{password}/{code}")
    public ICommonResponse register(@PathVariable String email, @PathVariable String password, @PathVariable String code, HttpServletResponse response) throws Exception {


        ICommonResponse iCommonResponse = userInfoService.addUserInfo( code,response,email,password);
        System.out.println("啦啦啦啦啦啦");
        return iCommonResponse;

    }

    //是否已注册，根据邮箱判断,true代表已经注册过，false代表尚未注册
    @GetMapping("/isRegistered/{email}")
    public boolean isRegistered(@PathVariable String email) {

        UserInfo userInfos = userInfoService.findUserInfo(email);

        if (userInfos!=null){
            return true;
        }else {
            return false;
        }

    }

    //登录接⼝，验证⽤户名密码合法性，根据⽤户名和密码⽣成token，token存⼊数据库，并写⼊cookie中，登录成功返回邮箱地址，重定向到欢迎⻚
    @PostMapping("/login/{email}/{password}")
    public ICommonResponse login(HttpServletResponse httpServletResponse,@PathVariable String email, @PathVariable String password) throws Exception {

        UserInfo userInfo = userInfoService.findUserInfo(email);

        if (userInfo.getPassword().equals(password)){

            LagouToken lagouToken = new LagouToken();
            lagouToken.setToken(UUID.randomUUID().toString());
            lagouToken.setEmail(email);
            lagouTokenService.saveToken(lagouToken);
            Cookie cookie = new Cookie("token",lagouToken.getToken());
            //不设置这个浏览器获取不到cookie
            cookie.setPath("/");
            cookie.setDomain("localhost");
            httpServletResponse.addCookie(cookie);
            return CommonRespBuilder.builder().createSuccessResp().setDatas(email).build();

//            Result result = new Result();
//            result.setB(true);
//            result.setMessage("登录成功！"+ lagouToken.getToken());
//            return result;
        }else {
//            Result result = new Result();
//            result.setB(false);
//            result.setMessage("登录失败，邮箱或密码输入错误");
//            return result;
            throw new RuntimeException("用户登录失败！账号/密码错误!");

        }

    }


//     根据token查询⽤户登录邮箱接⼝

    @PostMapping("/info/{token}")
    public ICommonResponse info(@PathVariable String token ) throws Exception {


        LagouToken tokenOne = lagouTokenService.findMailByToken(token);

        if (tokenOne!=null){
            return CommonRespBuilder.builder().createSuccessResp().setDatas(tokenOne.getEmail()).build();

        }else {
            throw new RuntimeException("根据token未查询到对应的用户，请重新登录!");
        }

    }
}
